/*******************************************************************************
 * MailGroupServiceDelegator.java
 *
 * Created by Jiagar Mistry
 * Last modified Date: Nov 10, 2008 5:00:51 PM by nishan
  * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ******************************************************************************/

package com.printeverywhere.delegator;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.printeverywhere.dto.GroupUser;
import com.printeverywhere.dto.Groups;
import com.printeverywhere.dto.GroupuserId;
import com.printeverywhere.dto.User;
import com.printeverywhere.util.HibernateUtil;

/**
 * The Class MailGroupServiceDelegator.
 */
public class MailGroupServiceDelegator {

	/**
	 * Gets the Group list.
	 *
	 * @param groupId the group id
	 *
	 * @return the list
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static Groups getGroupById(int groupId) throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(Groups.class);
			criteria.add(Restrictions.eq("groupId", groupId));
			List<Groups> groupList = criteria.list();
			transaction.commit();
			return (groupList.size() == 0) ? new Groups() : groupList.get(0);
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}

	public static Groups getGroupByStatus(Boolean status) throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(Groups.class);
			criteria.add(Restrictions.eq("status", status));
			List<Groups> groupList = criteria.list();
			transaction.commit();
			return (groupList.size() == 0) ? new Groups() : groupList.get(0);
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}

	/**
	 * Gets the group by name.
	 *
	 * @param groupName the group name
	 *
	 * @return the group by name
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static Groups getGroupByName(String groupName) throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(Groups.class);
			criteria.add(Restrictions.eq("name", groupName));
			List<Groups> groupList = criteria.list();
			transaction.commit();
			return (groupList.size() == 0) ? null : groupList.get(0);
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}

	/**
	 * Insert mail group.
	 *
	 * @param group the group
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static void insertMailGroup(Groups group) throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			session.save(group);
			transaction.commit();
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}

	/**
	 * Gets the Group list.
	 *
	 * @return the list
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static List<Groups> getGroupList() throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			List<Groups> groupList = session.createCriteria(Groups.class).list();
			transaction.commit();
			return groupList;
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}


	/**
	 * Gets the group user list.
	 *
	 * @param groupId the group id
	 *
	 * @return the group user list
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static List<GroupUser> getGroupUserList(int groupId) throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(GroupUser.class);
			criteria.addOrder(Order.asc("id.user.id"));
			criteria.add(Restrictions.eq("id.groups.groupId", groupId));
			List<GroupUser> groupUserList = criteria.list();
			transaction.commit();
			return groupUserList;
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}

	/**
	 * Delete group.
	 *
	 * @param group the group
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static void deleteGroup(Groups group) throws HibernateException {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try{
			session.delete(group);
			transaction.commit();
		}catch(HibernateException e){
			transaction.rollback();
			throw e;
		}finally{
			session.close();
		}
	}

	/**
	 * Count group subscriber by id.
	 *
	 * @param groupId the group id
	 *
	 * @return the int
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static int countGroupSubscriberById(int groupId) throws HibernateException {
		int totalSubscribers = 0;
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(GroupUser.class);
			criteria.add(Restrictions.eq("id.groups.groupId", groupId));
			totalSubscribers = Integer.parseInt(criteria.setProjection(Projections.rowCount()).uniqueResult().toString());
			transaction.commit();
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
		return totalSubscribers;
	}

	/**
	 * Gets the group user.
	 *
	 * @param groupId the group id
	 * @param userEmail the user email
	 *
	 * @return the group user
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static GroupUser getGroupUser(int groupId, String userEmail) throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(GroupUser.class);
			criteria.add(Restrictions.eq("groups", new Groups(groupId)));
			criteria.add(Restrictions.eq("userEmail", userEmail));
			List<GroupUser> groupUserList = criteria.list();
			transaction.commit();
			return (groupUserList.size() == 0) ? new GroupUser() : groupUserList.get(0);
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}

	public static List<GroupUser> getGroupUserByGroupId(int groupId) throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(GroupUser.class);
			criteria.add(Restrictions.eq("id.groups.groupId", groupId));
			List<GroupUser> groupUserList = criteria.list();
			transaction.commit();
			return groupUserList;
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}

	public static GroupUser getGroupUserByIdAndUser(Groups groups, User user) throws HibernateException {
		Session session = HibernateUtil.getSession();
		try{
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(GroupUser.class);
			criteria.add(Restrictions.eq("id.groups.groupId", groups));
			criteria.add(Restrictions.eq("id.groups.user", user));
			List<GroupUser> groupUserList = criteria.list();
			transaction.commit();
			if(groupUserList.size()>0)return groupUserList.get(0); else return null;
		}catch(HibernateException e){
			throw e;
		}finally{
			session.close();
		}
	}

	/**
	 * Insert mail group.
	 *
	 * @param groupUser the group user
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static void insertSubscriberToGroup(GroupUser groupUser) throws HibernateException {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try{
			session.saveOrUpdate(groupUser);
			transaction.commit();
		}catch(HibernateException e){
			transaction.rollback();
			throw e;
		}finally{
			session.close();
		}
	}

	public static void updateGroup(Groups groups) throws HibernateException {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try{
			session.update(groups);
			transaction.commit();
		}catch(HibernateException e){
			transaction.rollback();
			throw e;
		}finally{
			session.close();
		}
	}


	/**
	 * Delete mail group.
	 *
	 * @param groupUser the group user
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static void deleteSubscriberFromGroup(GroupUser groupUser) throws HibernateException {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try{
			session.delete(groupUser);
			transaction.commit();
		}catch(HibernateException e){
			transaction.rollback();
			throw e;
		}finally{
			session.close();
		}
	}

	/**
	 * Delete mail group.
	 *
	 * @param strQuery the str query
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static void deleteGroupUser(String strQuery) throws HibernateException {
		Session session = HibernateUtil.getSession();
		Transaction transaction = session.beginTransaction();
		try{
			Query query = session.createQuery(strQuery);
		    int row = query.executeUpdate();
			transaction.commit();
		}catch(HibernateException e){
			transaction.rollback();
			throw e;
		}finally{
			session.close();
		}
	}
}